package employee

import (
	"context"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/employee/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"
	"gitee.com/liangziyisheng/caas-platform-mc/utility/utils"
)

func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	tenantCode := ctx.Value(consts.TokenClaims.TenantCode).(string)
	in := &v1.ListInput{}
	utils.CopyStruc(*req, in)
	in.TenantCode = tenantCode
	out, err := service.Employee().List(ctx, in)
	if err != nil {
		return nil, err
	}
	res = &v1.ListRes{Total: out.Total, Data: make([]v1.Emp, 0)}
	for _, v := range out.Data {
		e := v1.Emp{
			Id:           v.Id,
			EmployeeNo:   v.EmployeeNo,
			FirstName:    v.FirstName,
			LastName:     v.LastName,
			Gender:       v.Gender,
			Birthday:     v.Birthday,
			NationalId:   v.NationalId,
			Email:        v.Email,
			Phone:        v.Phone,
			HireDate:     v.HireDate,
			PositionId:   v.PositionId,
			DepartmentId: v.DepartmentId,
			ManagerId:    v.ManagerId,
			CountryCode:  v.CountryCode,
			ProvinceCode: v.ProvinceCode,
			CityCode:     v.CityCode,
			Address:      v.Address,
			PostalCode:   v.PostalCode,
			Status:       v.Status,
		}
		res.Data = append(res.Data, e)
	}
	return res, nil
}
